package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.BagCustmizedInfo;
import com.joplus.housekeeper.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * author： wutingxi
 * time： 2017/2/14.
 * description：
 */

public class HorizontalScrollViewAdapter {


    private Context mContext;
    private LayoutInflater mInflater;
    private List<BagCustmizedInfo.DataBean.DictListBean> mDatas;
    private int selectIndex = -1;//选中的序号

    Map<Integer, ImageView> bagViewMap = new HashMap<>();

    OnBagItemClickListener onBagItemClickListener;

    public void setOnBagItemClickListener(OnBagItemClickListener onBagItemClickListener) {
        this.onBagItemClickListener = onBagItemClickListener;
    }

    public HorizontalScrollViewAdapter(Context context, List<BagCustmizedInfo.DataBean.DictListBean> mDatas) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mDatas = mDatas;
    }

    public int getCount() {
        //实际的数量要多一个用户自己填写的
        return mDatas.size() + 1;
    }

    public Object getItem(int position) {
        return mDatas.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (position < getCount() - 1) {//当前不是最后一个硬加的用户输入的控件
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_bag_size, null, false);
                final ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_bagIcon);
                imageView.setAlpha(0.5f);
                bagViewMap.put(position, imageView);
                TextView sizeName = (TextView) convertView.findViewById(R.id.tv_sizename);
                if (mDatas.get(position) != null) {
                    ImageLoader.getInstance().displayImage(mDatas.get(position).getDictCategory(), imageView);
                }
                if (mDatas.get(position) != null) {
                    StringUtils.setText(sizeName, mDatas.get(position).getName());
                }
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onBagItemClickListener != null) {
                            if (selectIndex == position) {
                                return;//一样，什么都不做
                            }
                            Set<Integer> pos = bagViewMap.keySet();
                            for (int i : pos) {
                                if (i != position) {
                                    bagViewMap.get(i).setAlpha(0.5f);
                                } else {
                                    bagViewMap.get(i).setAlpha(1f);
                                }
                            }
                            onBagItemClickListener.onClick(mDatas.get(position).getName(), false);
                        }
                    }
                });
                convertView.setTag(viewHolder);
            }
        } else {//这个是硬加的控件
            convertView = mInflater.inflate(R.layout.item_bag_size_input, null, false);
            final ImageView imageView = (ImageView) convertView.findViewById(R.id.ic_sizeicon);
            imageView.setAlpha(0.5f);
            bagViewMap.put(position, imageView);
            final EditText sizeName = (EditText) convertView.findViewById(R.id.ed_input_size);
            if (mDatas.get(position - 2) != null) {
                ImageLoader.getInstance().displayImage(mDatas.get(position - 2).getDictCategory(), imageView);
            }
            sizeName.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    if(s!=null&&(s.toString()).length()>0){
                        onBagItemClickListener.onClick(s.toString(), true);
                    }else{
                        onBagItemClickListener.onClick("",true);
                    }
                }
            });
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBagItemClickListener != null) {
                        if (selectIndex == position) {
                            return;//一样，什么都不做
                        }
                        sizeName.setFocusable(true);
                        sizeName.requestFocus();
                        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

                        Set<Integer> pos = bagViewMap.keySet();
                        for (int i : pos) {
                            if (i != position) {
                                bagViewMap.get(i).setAlpha(0.5f);
                            } else {
                                bagViewMap.get(i).setAlpha(1f);
                            }
                        }
                    }
                }
            });


        }

        return convertView;
    }

    private class ViewHolder {
        ImageView mImg;
        TextView mText;
    }

    public void clear() {
        selectIndex = -1;
        Set<Integer> pos = bagViewMap.keySet();
        for (int i : pos) {
            bagViewMap.get(i).setAlpha(0.5f);
        }
    }


    public interface OnBagItemClickListener {
        void onClick(String size, Boolean isSysDef);
    }
}
